package cn.xuqiudong.backend.module.system.user.controller;

import cn.xuqiudong.backend.base.controller.BaseBackendController;
import cn.xuqiudong.backend.module.system.user.model.SysUser;
import cn.xuqiudong.backend.module.system.user.service.SysUserService;
import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.util.encrypt.PasswordUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 功能: :系统用户表 控制层
 *
 * @author Vic.xu
 * @since 2025-02-07 16:47
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseBackendController<SysUserService, SysUser> {
    /**
     * 修改密码
     */
    @PostMapping(value = "/updatePassword/{id}")
    public BaseResponse<Boolean> updatePassword(@PathVariable Integer id, String password) {
        return BaseResponse.success(service.updatePassword(id, password) > 0);
    }

    /**
     * 新增或修改
     */
    @PostMapping(value = "/save")
    public BaseResponse<Integer> save(@RequestBody SysUser entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            //修改的时候 不更新密码
            entity.setPassword(null);
        } else {
            // 新增的时候设置默认密码 为用户名
            entity.setPassword(PasswordUtils.entryptPassword(entity.getUsername()));
        }
        return BaseResponse.success(service.save(entity));
    }


    /**
     * 修改用户状态
     */
    @PostMapping(value = "/updateEnable/{id}")
    public BaseResponse<Integer> updateEnable(@PathVariable Integer id, Boolean enable) {
        SysUser entity = new SysUser();
        entity.setId(id);
        entity.setEnable(enable);
        return BaseResponse.success(service.update(entity));
    }

    /**
     * 检测用户名是否可用
     */
    @PostMapping(value = "/checkUsername")
    public BaseResponse<Boolean> checkUsername(String username) {
        return BaseResponse.success(service.checkNotRepeat(null, username, "username"));
    }



}
